/**
 * 
 */
//map.setCenter();

var paths=[{lng:[[104.057449 ,30.651296 ],
                 [104.060377 ,30.651726 ],
                 [104.061341 ,30.654098 ],
                 [104.062985 ,30.654202 ],
                 [104.063568 ,30.654724 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[103.972006 ,30.622635 ],
                 [104.041769 ,30.647857 ],
                 [104.056252 ,30.653628 ],
                 [104.057216 ,30.654200 ],
                 [104.059162 ,30.654171 ],
                 [104.061341 ,30.654098 ],
                 [104.062381 ,30.654696 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.055640 ,30.656048 ],
                 [104.057216 ,30.654200 ],
                 [104.057765 ,30.655111 ],
                 [104.059994 ,30.657299 ],
                 [104.060885 ,30.655290 ],
                 [104.065171 ,30.655870 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.053449 ,30.660317 ],
                 [104.059994 ,30.657299 ],
                 [104.062716 ,30.656833 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.061273 ,30.657567 ],
                 [104.062716 ,30.656833 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.030217 ,30.673338 ],
                 [104.042499 ,30.666440 ],
                 [104.057391 ,30.659726 ],
                 [104.060554 ,30.658758 ],
                 [104.061273 ,30.657567 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.054034 ,30.666954 ],
                 [104.059180 ,30.661596 ],
                 [104.060554 ,30.658758 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.024836 ,30.702747 ],
                 [104.046993 ,30.681833 ],
                 [104.057356 ,30.673845 ],
                 [104.061977 ,30.664856 ],
                 [104.059283 ,30.663186 ],
                 [104.062373 ,30.659880 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.060480 ,30.669425 ],
                 [104.061977 ,30.664856 ],
                 [104.065279 ,30.664818 ],
                 [104.065348 ,30.663178 ],
                 [104.064830 ,30.658608 ],
                 [104.065300 ,30.656265 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.068084 ,30.670635 ],
                 [104.066819 ,30.665077 ],
                 [104.065348 ,30.663178 ],
                 [104.065755 ,30.657517 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.078248 ,30.689968 ],
                 [104.073063 ,30.685981 ],
                 [104.071082 ,30.664463 ],
                 [104.066397 ,30.659963 ],
                 [104.065755 ,30.657517 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.078000 ,30.668531 ],
                 [104.075496 ,30.663991 ],
                 [104.071082 ,30.664463 ],
                 [104.069329 ,30.660774 ],
                 [104.067412 ,30.657992 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.086027 ,30.667432 ],
                 [104.075496 ,30.663991 ],
                 [104.074803 ,30.660146 ],
                 [104.072376 ,30.660171 ],
                 [104.066277 ,30.656266 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.074803 ,30.660146 ],
                 [104.071610 ,30.658372 ],
                 [104.069349 ,30.657596 ],
                 [104.068607 ,30.656700 ],
                 [104.066620 ,30.656168 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.079614 ,30.658700 ],
                 [104.076993 ,30.657941 ],
                 [104.070955 ,30.656827 ],
                 [104.068607 ,30.656700 ],
                 [104.066482 ,30.656114 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.102827 ,30.662332 ],
                 [104.085204 ,30.659206 ],
                 [104.070955 ,30.656827 ],
                 [104.067091 ,30.656109 ],
                 [104.066573 ,30.655935 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.079577 ,30.656076 ],
                 [104.076742 ,30.656580 ],
                 [104.075280 ,30.655849 ],
                 [104.072921 ,30.655839 ],
                 [104.066573 ,30.655935 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.078833 ,30.654419 ],
                 [104.073861 ,30.654155 ],
                 [104.072921 ,30.655839 ],
                 [104.068126 ,30.655480 ],
                 [104.067945 ,30.655273 ],
                 [104.067639 ,30.655302 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.076181 ,30.653263 ],
                 [104.070842 ,30.654457 ],
                 [104.067639 ,30.655302 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.096913 ,30.644505 ],
                 [104.070842 ,30.654457 ],
                 [104.069219 ,30.654856 ],
                 [104.067781 ,30.654934 ],
                 [104.067378 ,30.655125 ],
                 [104.067082 ,30.655340 ],
                 [104.066110 ,30.655580 ],
                 [104.065589 ,30.655749 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.080078 ,30.649168 ],
                 [104.068280 ,30.654680 ],
                 [104.067781 ,30.654934 ],
                 [104.067476 ,30.654737 ],
                 [104.067481 ,30.654964 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.160624 ,30.600876 ],
                 [104.071843 ,30.651711 ],
                 [104.069311 ,30.653027 ],
                 [104.067476 ,30.654737 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.070071 ,30.652332 ],
                 [104.069311 ,30.653027 ],
                 [104.068336 ,30.653611 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.078341 ,30.638803 ],
                 [104.070312 ,30.648340 ],
                 [104.068336 ,30.653611 ],
                 [104.066561 ,30.654492 ],
                 [104.066426 ,30.654973 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.070312 ,30.648340 ],
                 [104.067945 ,30.651177 ],
                 [104.067565 ,30.652227 ],
                 [104.066307 ,30.654075 ],
                 [104.065611 ,30.655310 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.068529 ,30.641260 ],
                 [104.066655 ,30.648522 ],
                 [104.066792 ,30.652972 ],
                 [104.065611 ,30.655310 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.071184 ,30.622534 ],
                 [104.065493 ,30.632991 ],
                 [104.066655 ,30.648522 ],
                 [104.066274 ,30.650155 ],
                 [104.065465 ,30.654018 ],
                 [104.065289 ,30.654643 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.061234 ,30.644275 ],
                 [104.060764 ,30.648782 ],
                 [104.063030 ,30.650872 ],
                 [104.065095 ,30.652251 ],
                 [104.065289 ,30.654643 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[104.053179 ,30.639562 ],
                 [104.056941 ,30.645596 ],
                 [104.063440 ,30.653051 ],
                 [104.064178 ,30.654524 ],
                 [104.065349 ,30.655826 ]]},

                 {lng:[[103.981050 ,30.575130 ],
                 [104.062402 ,30.653123 ],
                 [104.065349 ,30.655826 ]]},
];
var rr;
function Dragroutetest(path){
			map.plugin("AMap.DragRoute",function(){
				route = new AMap.DragRoute(map, path, AMap.DrivingPolicy.LEAST_DISTANCE,{
						midMarkerOptions:{
							//visible :false,	
						},
						startMarkerOptions:{
							//visible :false,	
						},
						endMarkerOptions:{
							//visible :false,	
						},
						polyOptions :{
							strokeColor:'#cc9',
							strokeOpacity:0.5
						}
					}); 
				//构造拖拽导航类，传入参数分别为：地图对象，初始路径，驾车策略
				route.search(); //查询导航路径并开启拖拽导航
				//当路径完成时的事件
				console.log("enter serach-----");
				AMap.event.addListener(route,'complete',function(e){
				    var r=e.data.routes[0].steps;
					for(i=0;i<r.length;i++){
					     //各路段描述
						//console.log(e.data.routes[0].steps[i].instruction );
						//目标路程
					//	console.log(e.data.routes[0].distance);
						//预计时间
					//	console.log(e.data.routes[0].time);
						//console.log("enter complete-----");
					}
					console.log(e.data.routes[0].distance);
					//预计时间
					console.log(e.data.routes[0].time);
					$('#result').css("display","inline");
					$('#addroute').css("display","none");
					var trs=document.getElementById("routenumber");
					trs.innerHTML="线路"+$('#satation-search input').val();
					var tds=document.getElementById("time-distance").getElementsByTagName("td");
					tds[0].innerHTML=e.data.routes[0].time/60+"分钟";
					tds[1].innerHTML=e.data.routes[0].distance/1000+"千米";
					var s_e=document.getElementById("start-end");
				});
				
				//s_e.innerHTML=satations[0].name+"-----"+satations[satations.length-1].name;
			});
			rr=route;
}	
for(var i=0;i<paths.length;i++){
	//Dragroutetest(paths[i].lng);
	//console.log(paths[i].loc);
}

var flag=1;
$('#satation-search input').bind('input oninput',function(){
	var index=$('#satation-search input').val();
	if(index>=0&&index<paths.length){ 
		if(flag!=1){rr.destroy();flag=2;}
		map.clearMap();
		Dragroutetest(paths[index].lng);
		flag=2;
	}
});
//Dragroutetest(paths[10].lng);
//Dragroutetest(paths[7].lng);